
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:self32/entity/book_entity.dart';
import 'package:self32/entity/print_entity.dart';

import '../../main.dart';
import '../../util/printer.dart';
import '../printing_dialog.dart';

class ResultModel{

  final RxList<BookEntity> _list = RxList();

  int type = 0;

  bool canDone = false;

  setAble(){
    canDone = true;
  }

  _listCell(text,{int width = 90, int height = 20, Color color =Colors.black }){
    return Container(
        alignment: Alignment.center,
        width: width.w,
        height: height.h,
        child: Text('$text',style: TextStyle(color:color, fontSize: 8.sp),textAlign: TextAlign.center,)
    );
  }

  _labelRow(width){
    return  Container(
      height: 30.h,
      decoration: BoxDecoration(
        color: const Color.fromRGBO(219, 232, 255, 1),
        borderRadius: BorderRadius.only(topLeft:Radius.circular(4.r),topRight:Radius.circular(4.r)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          _listCell("序号",width: ((50/510)*width).toInt()),
          _listCell("书籍名称",width: ((140/510)*width).toInt()),
          _listCell("书籍编号",width: ((140/510)*width).toInt()),
          _listCell("应还时间",width: ((100/510)*width).toInt()),
          _listCell("状态",width: ((80/510)*width).toInt()),
        ],
      ),
    );
  }

  Widget show(list,type,{int width = 510,int marginLeft = 10}){
    this.type = type;
    _list.clear();
    _list.addAll(list);
    return  Obx(() => Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(4.r)),
      ),
      margin: EdgeInsets.only(left: marginLeft.w),
      width: width.w,
      height: 310.h,
      child: Column(
        children: [
          _labelRow(width),
          SizedBox(height:4.h),
          SizedBox(
              height: 220.h,
              child: ListView.separated(
                itemBuilder: (context, index) {
                  return _itemBuilder(index,width);
                },
                itemCount: _list.length,
                separatorBuilder: (BuildContext context, int index) {
                  return Divider(
                    color: Colors.grey[400],
                  );
                },
              )
          ),
          SizedBox(height:10.h),
          InkWell(
            onTap: ()  {
              if(!canDone){
                return;
              }
              currentTask.clear();
              timer.cancel();
              Get.until((route) => route.isFirst);

              // var printList = _list.where((ele) => ele.state == 1).toList();

              // if(printList.isNotEmpty){
              //   PrintingDialog().show((){
              //     var entity = PrintEntity()
              //       ..uid = currentTask.uid
              //       ..uName = currentTask.uName
              //       ..type = type;
              //     currentTask.clear();
              //     timer.cancel();
              //     Get.until((route) => route.isFirst);
              //     Print().printing(entity,_list.where((ele) => ele.state == 1 || ele.state ==-1).toList());
              //   });
              // }else{
              //   Get.until((route) => route.isFirst);
              // }
            },
            child: Image.asset("assets/images/done.png",width: 100.w,fit: BoxFit.fitWidth,),
          )
        ],
      ),
    ));
  }

  _itemBuilder(index,width){
    var item = _list[index];
    return Row(
      children: [
        _listCell("${index+1}",width: ((50/510)*width).toInt()),
        _listCell("《${item.mainTitle}》",width: ((140/510)*width).toInt()),
        _listCell("${item.no}",width: ((140/510)*width).toInt()),
        _listCell(item.readyReturnDate??'',width: ((100/510)*width).toInt()),
        Container(
          width: ((80/510)*width).toInt().w,
          alignment: Alignment.center,
          child:  Tooltip(
            message: item.error??'',
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if(type == 0)...[
                  Image.asset("assets/images/${item.state==1?'borrow_success':item.state==2?'has_borrowed':item.state==0?'borrow_wait':'borrow_fail'}.png",width: 60.w,fit: BoxFit.fitWidth,),
                ]else...[
                  Image.asset("assets/images/${item.state==1?'return_success':item.state==2?'has_returned':item.state==0?'return_wait':'return_fail'}.png",width: 60.w,fit: BoxFit.fitWidth,),
                ]
              ],
            ),
          ),
        ),
      ],
    );
  }
}

